package com.example.smartcamera.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import com.example.smartcamera.ResourceTable;
import ohos.agp.components.ListContainer;
import ohos.agp.components.TabList;
import ohos.app.Context;

import com.example.smartcamera.component.PictureAdapter;
import com.example.smartcamera.utils.Utils;


import ohos.agp.components.Component;
import ohos.media.image.PixelMap;

import java.util.List;

public class PhotoAbilitySlice extends AbilitySlice {
    private static final int TAB_WIDTH = 64;
    private static final int TAB_PADDING_LEFT = 12;
    private static final int TAB_PADDING_RIGHT = 12;
    private static final int TAB_PADDING_BOTTOM = 12;
    private static final int TAB_PADDING_TOP = 12;
    private TabList tabList1;

    //复制过来的
    private static final int DELAY_TIME = 10; // 延迟执行
    private static ListContainer listContainer;
    private static Context context;
    private static PictureAdapter pictureAdapter; // 图片适配器类

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_photo);

        //复制过来的
        context = getContext();
        // 设置宽度
        Utils.setLeftWidth(context);
        // 计算屏幕总宽度
        Utils.calSumWidth();
        // Initializing the listContainer Component
        //初始化布局，各种按钮
        initComponents();
        addTabSelectedListener();
//        initListContainer();
        // Converts images in Media to List<PixelMap>. The default size is used
        Utils.transResourceIdsToListOnce(context);
        getUITaskDispatcher().delayDispatch(() -> initData(Utils.transIdToPixelMap(context)), DELAY_TIME);
    }


    /**
     * Image data in listContainer
     * The RightAbilitySlice in the parallel horizon is initialized after the RightAbilitySlice is initialized.
     * Therefore, the width of the picture in the listContainer is calculated in the RightAbilitySlice,
     * and then this method is called to set the picture.
     *
     * @param pixelListArray pixelListArray
     */
    public static void initData(List<PixelMap[]> pixelListArray) {
        context.getUITaskDispatcher().asyncDispatch(() -> {
            listContainer.setItemProvider(pictureAdapter);
            pictureAdapter.replace(pixelListArray);
        });
    }


    //初始化导航栏
    private void initTab() {
        if (tabList1.getTabCount() == 0) {
            tabList1.addTab(createTab("实时"));
            tabList1.addTab(createTab("相册"));
            tabList1.addTab(createTab("回放"));
            tabList1.addTab(createTab("我的"));
            tabList1.setFixedMode(true);
            tabList1.getTabAt(1).select();
            tabList1.getTabAt(0).setClickedListener(lisener->present(new RealTimeAbilitySlice() , new Intent()));
            tabList1.getTabAt(1).setClickedListener(lisener->present(new PhotoAbilitySlice() , new Intent()));
            tabList1.getTabAt(2).setClickedListener(lisener->present(new PlayBackAbilitySlice() , new Intent()));
            tabList1.getTabAt(3).setClickedListener(lisener->present(new MineAbilitySlice() , new Intent()));
        }
    }

    //添加点击功能（在这里做ability跳转）
    private void addTabSelectedListener() {
        tabList1.addTabSelectedListener(new TabList.TabSelectedListener() {
            @Override
            public void onSelected(TabList.Tab tab) {
            }

            @Override
            public void onUnselected(TabList.Tab tab) {
            }

            @Override
            public void onReselected(TabList.Tab tab) {
            }
        });
    }

    //创建下方导航栏
    private TabList.Tab createTab(String text) {
        TabList.Tab tab = tabList1.new Tab(this);
        tab.setText(text);
        tab.setMinWidth(TAB_WIDTH);
        tab.setPadding(TAB_PADDING_LEFT, TAB_PADDING_TOP, TAB_PADDING_RIGHT, TAB_PADDING_BOTTOM);
        return tab;
    }

    private void initComponents() {
        tabList1 = (TabList) findComponentById(ResourceTable.Id_tab_list_photo);
        initTab();
        pictureAdapter = new PictureAdapter(null, context);
        Component component = findComponentById(ResourceTable.Id_image_list);
        if (component instanceof ListContainer) {
            listContainer = (ListContainer) component;
            listContainer.setItemProvider(pictureAdapter);
        }

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
